package com.lou.springboot.controller.admin;

import com.lou.springboot.common.PageResult;
import com.lou.springboot.common.ResultVO;
import com.lou.springboot.common.ResultVOUtil;
import com.lou.springboot.enums.ResultEnum;
import com.lou.springboot.service.CommentService;
import com.lou.springboot.utils.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@Controller
@RequestMapping("/admin")
public class CommentController {

    @Autowired
    private CommentService commentService;

    @GetMapping("/comments")
    public String list(HttpServletRequest request){
        request.setAttribute("path","comments");
        return "admin/comment";
    }

    @GetMapping("/comments/list")
    @ResponseBody
    public ResultVO list(@RequestParam Map<String,Object> map) {
        if (StringUtils.isEmpty(map.get("page")) && StringUtils.isEmpty(map.get("limit"))) {
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        PageUtil pageUtil = new PageUtil(map);
        return ResultVOUtil.success(commentService.getCommentsPage(pageUtil));
    }

    @PutMapping("/comments/checkDone")
    @ResponseBody
    public ResultVO checkDone(@RequestBody Integer[] commentIds) {
        if (commentIds.length < 1) {
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        if (commentService.done(commentIds)) {
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error("审核失败");
        }
    }

    @PostMapping("/comments/reply")
    @ResponseBody
    public ResultVO checkDone(@RequestParam("commentId") Long commentId,
                              @RequestParam("replyBody") String replyBody) {
        if (commentId == null || commentId < 1 || StringUtils.isEmpty(replyBody)) {
            return ResultVOUtil.error(ResultEnum.PARAM_ERROR);
        }
        if (commentService.reply(commentId, replyBody)) {
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error("回复失败");
        }
    }

    @PostMapping("/comments/delete")
    @ResponseBody
    public ResultVO delete(@RequestBody Integer[] ids) {
        if (ids.length < 1) {
            return ResultVOUtil.error("参数异常！");
        }
        if (commentService.deleteBatch(ids)) {
            return ResultVOUtil.success();
        } else {
            return ResultVOUtil.error("刪除失败");
        }
    }
}
